<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class SystemInfo extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_v2_system_info';

    protected $append = [

        'hours',

        'day'
    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-08-23 17:05
     */
    public function getDayAttr($value,$data){

        if(!empty($data['create_time'])){

            return infoFriendTime($data['create_time']);

        }

    }

    /**
     * @param $value
     * @param $data
     * @功能说明:获取时间
     * @author chenniang
     * @DataTime: 2022-08-23 16:36
     */
    public function getHoursAttr($value,$data){

        if(!empty($data['create_time'])){

            return date('H:i:s',$data['create_time']);

        }

    }


    /**
     * @param $type
     * @param string $order_code
     * @param string $goods_name
     * @功能说明:获取系统消息拼接信息
     * @author chenniang
     * @DataTime: 2022-08-22 14:44
     */
    public function returnMsg($type,$data){

        switch ($type){

            case 1://提现成功

                $data['msg']   = '提现成功！'.$data['true_price'].'元已提现到您的支付宝，请到支付宝查收';

                $data['title'] = '提现通知';

                break;

            case 2://提现失败

                $data['msg']   = '提现失败！管理员拒绝您的提现申请';

                $data['title'] = '提现通知';

                break;

            case 3://商城下单

                $data['msg']   = '您下单的订单已支付成功，等待卖家发货';

                $data['title'] = '商城订单下单成功通知';

                break;

            case 4://商场发货

                $data['msg']   = '您购买的订单已发货，预计5个工作日内送达。如遇特殊情况，可联系客服查询物流情况';

                $data['title'] = '商城订单发货通知';

                break;

            case 5://土地下单

                $data['msg']   = '您购买的土地农作物订单已生效';

                $data['title'] = '土地订单下单成功通知';

                break;

            case 6://商场退款

                $data['msg']   = '您的订单已退款成功';

                $data['title'] = '商城订单退货通知';

                break;

            case 7://认养到期可配送

                $data['msg']   = '您下单的（'.$data['goods_name'].'），快去发起配送请求吧';

                $data['title'] = '认养订单配送通知';

                break;

            case 8://认养下单

                $data['msg']   = '您下单的（'.$data['goods_name'].'）,已支付成功';

                $data['title'] = '认养订单下单通知';

                break;

            case 9://土地到期

                $data['msg']   = '您租赁的土地，租赁到期时间为'.$data['end_time'];

                $data['title'] = '土地租赁到期通知';

                break;

            case 10://积分

                $data['msg']   = '您通过消费获得'.$data['get_integral'].'积分';

                $data['title'] = '积分变动提醒';

                break;

            case 11://兑换商品消耗积分

                $data['msg']   = '您已成功兑换积分商品，消耗'.$data['integral'].'积分';

                $data['title'] = '积分变动提醒';

                break;
            case 12://抽奖消耗积分

                $data['msg']   = '您今日参与第'.$data['times'].'次积分抽奖，消耗'.$data['integral'].'积分';

                $data['title'] = '积分变动提醒';

                break;
            case 13://签到获取积分

                $data['msg']   = '您已连续签到'.$data['times'].'天，成功获得'.$data['integral'].'积分';

                $data['title'] = '签到提醒';

                break;

            case 14://签到

                $data['msg']   = '你今日还未签到，快去签到吧';

                $data['title'] = '签到提醒';

                break;
            case 15://秒杀

                $data['msg']   = '您预约的秒杀活动还有2分钟即将开始';

                $data['title'] = '秒杀提醒';

                break;
            case 16://秒杀

                $data['msg']   = '储值成功，恭喜您成为'.$data['member_title'].'会员';

                $data['title'] = '充值提醒';

                break;
        }

        return $data;
    }








    /**
     * @param $data
     * @param $type
     * @功能说明:
     * @author chenniang
     * @DataTime: 2022-08-22 14:46
     */
    public function infoAdd($data,$type){

        $arr = $this->returnMsg($type,$data);
        //商城订单需要获取第一个封面图
        if(in_array($type,[3,4,6])){

            $data['goods_cover'] = !empty($data['order_goods'][0]['goods_cover'])?$data['order_goods'][0]['goods_cover']:'';
        }

        $insert = [

            'uniacid' => $data['uniacid'],

            'user_id' => $data['user_id'],

            'order_code' => !empty($data['order_code'])?$data['order_code']:'',

            'goods_cover'=> !empty($data['goods_cover'])?$data['goods_cover']:'',

            'type'    => $type,

            'obj_id'  => $data['id'],

            'msg'     => $arr['msg'],

            'title'   => $arr['title'],
        ];

        $res = $this->dataAdd($insert);

        return $res;
    }




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2022-08-23 17:29
     * @功能说明:用户是否有未读消息
     */
    public function userHaveNews($user_id,$uniacid){

        $record_model = new InfoRecord();

        $operate_no_read_count = $record_model->noReadCount($user_id,$uniacid);

        $where[] = ['uniacid','=',$uniacid];

        $where[] = ['user_id','=',$user_id];

        $no_read_count = $this->where($where)->where(['status'=>0])->count();

        return $operate_no_read_count>0||$no_read_count>0?1:0;
    }







}